{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#\n",
    "1.以Lena为原始图像，通过OpenCV实现平均滤波，高斯滤波及中值滤波，比较滤波结果。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "end work\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "img=cv.imread(r'Lena.jpg')\n",
    "\n",
    "averImg = cv.blur(img,(5,5))\n",
    "GaussImg = cv.GaussianBlur(img,(5,5),0)\n",
    "\n",
    "medianImg=cv.medianBlur(img,5)\n",
    "\n",
    "cv.imshow('average',averImg)\n",
    "cv.imshow('Gauss',GaussImg)\n",
    "cv.imshow('median',medianImg)\n",
    "print('end work')\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###经过滤波发现对于均值滤波会显得比较模糊，图片整体细节信息出现了较大的缺失，因为均值滤波是对周围的像素求平均；中值滤波与高斯滤波整体没有太大区别，但是中值滤波对于相近的像素之间处理更明显，比如Lena帽子的毛发处复杂的纹理进行了处理显得模糊一体了。高斯滤波处理后感觉整副图案变浅了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#\n",
    "2.以Lena为原始图像，通过OpenCV使用Sobel及Canny算子检测，比较边缘检测结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "\n",
    "img=cv.imread(r'Lena.jpg')\n",
    "imgGray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)\n",
    "\n",
    "Sobel=cv.Sobel(img,cv.CV_8U,1,1)\n",
    "Canny=cv.Canny(img,100,200)\n",
    "Sobel_Gray=cv.Sobel(imgGray,cv.CV_8U,1,1)\n",
    "Canny_Gray=cv.Canny(imgGray,100,200)\n",
    "\n",
    "cv.imshow('Sobel',Sobel)\n",
    "cv.imshow('Canny',Canny)\n",
    "cv.imshow('SobelGray',Sobel_Gray)\n",
    "cv.imshow('CannyGtay',Canny_Gray)\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###使用发现Sobel基本算子监测会丢失很多细节，因为其对于渐变的难以识别（对于输入参数ddepth:图像的颜色深度，针对不同的输入图像，输出目标图像有不同的深度，有些不是很理解）；Canny可以基本识别出想要的边缘，因为其具有边缘连接功能效果看上去很好，但是对于两个阈值的确定现在没有很好地办法。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#\n",
    "3.在OpenCV安装目录下找到课程对应演示图片(安装目录\\sources\\samples\\data)，首先计算灰度直方图，进一步使用大津算法进行分割，并比较分析分割结果。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[128 161 161 ... 173 173 155]\n",
      " [128 161 161 ... 168 170 155]\n",
      " [128 162 163 ... 169 169 158]\n",
      " ...\n",
      " [128  40  41 ... 103 102 104]\n",
      " [128  41  43 ... 101 101 106]\n",
      " [144 144 144 ... 144 144 144]]\n",
      "[128 161 161 ... 144 144 144]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD4CAYAAAAAczaOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3de3zU1Z3/8dcnN8ItXMMdCkgQ8YIXirSKW7VegPaH2svai6VdLT626Grb326x1ta2ulK36tZft3axskLXlfqrF6iiFqmtpYpyvwtE5B4CGEgCgUCSs398vzOZSSbJJEwyl+/7+XjkMd85852Z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sD7gZWNTBdRARCawO7d5xztWY2R3A60A2MNc5t6mND9diF1Ca0nEFQ9B+H0E63pQ+VnPOtbyXiIhkBH0jV0QkQBT6IiIBkpahn85LOZjZXDM7aGYbI8p6m9kSM9vuX/byy83MHvePc72ZXZy8mjfPzIaa2ZtmtsXMNpnZXX552h9boqXz6zeWTH1NNyXdX+tpF/oZsJTD08D1DcpmAUudc0XAUv86eMdY5P/MAJ7ooDq2RQ3wXefcOcBEYKb/d8mEY0uYDHj9xvI0mfmabkpav9bTLvSJWMrBOXcKCC3lkBacc28BZQ2KpwHz/O15wA0R5fOdZznQ08wGdkxNW8c5V+KcW+1vVwJb8L6BnfbHlmBp/fqNJVNf001J99d6OoZ+rKUcBiepLonS3zlXAt4LCujnl6flsZrZcOAi4F0y7NgSICjHHYi/ezq+1tMx9FtcyiGDpN2xmlk34HngbudcRXO7xihL6WNLkKAed0jGHH+6vtbTMfQzcSmH0tDHPf/yoF+eVsdqZrl4/wTPOOde8Isz4tgSKCjHndF/93R+radj6GfiUg6LgOn+9nRgYUT51/zR/4lAeejjY6oxMwOeArY45x6NuCntjy3BMvH1G0vG/t3T/rXunEu7H2AKsA34ALg32fVpZd2fBUqA03gtgFuBPnij/dv9y97+voY30+MDYAMwPtn1b+a4Lsf7yLoeWOv/TMmEY2uH31Xavn6bOJ6MfE03c7xp/VrXMgwiIgGSjt07IiLSRgp9EZEAUeiLiASIQl9EJEAU+iIiAaLQFxEJEIW+iEiA/C8yUVi7TPpWSAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "img = cv.imread(r'Lena.jpg',cv.IMREAD_GRAYSCALE)\n",
    "HIST=cv.calcHist([img],[0],None,[256],[0,256])\n",
    "\n",
    "fig=plt.figure(111)\n",
    "fig1=fig.add_subplot(1,2,1)\n",
    "plt.hist(img.ravel(),256,[0,256])#ravel()将多维数组降到一维\n",
    "print(img)\n",
    "print(img.ravel())\n",
    "fig2=fig.add_subplot(1,2,2)\n",
    "plt.plot(img.ravel())\n",
    "plt.xlim(0,255)\n",
    "plt.show()#应用绘图绘制\n",
    "\n",
    "_,bw = cv.threshold(img,70,0xff,cv.THRESH_OTSU)#应用大津算法\n",
    "cv.imshow('Resource',img)\n",
    "cv.imshow('result',bw)\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###此处使用了python常用的绘图库matplotlib来绘制了灰度直方图，通过从opencv中imread到图片的二维数组然后使用ravel()转化为一维数组,应用大津算法进行图形分割后发现，对于颜色较浅的背景区域可以很好的去除，但是类似头发与门廊位置颜色渐变的就不容易区分了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.使用米粒图像，分割得到各米粒，首先计算各区域(米粒)的面积、长度等信息，进一步计算面积、长度的均值及方差，分析落在3sigma范围内米粒的数量。\n",
    "\n",
    "              详见rice.cpp，python关于方格绘制等正在学习中，希望老师可以给一个python完成的实例。还有上一题也是。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. 使用棋盘格及自选风景图像，分别使用SIFT、FAST及ORB算子检测角点，并比较分析检测结果。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "image = cv.imread(r'chessboard.png',cv.IMREAD_GRAYSCALE)#以灰度方式读取\n",
    "img=cv.resize(image,(0,0),fx=0.1,fy=0.1) #修改照片尺寸\n",
    "\n",
    "\n",
    "\n",
    "#Harris = cv.cornerHarris(img,2,3,0.04)\n",
    "#keypoint = cv.dilate(Harris,None)\n",
    "\n",
    "Sift = cv.xfeatures2d.SIFT_create()\n",
    "keypoint_1=Sift.detect(img,None)\n",
    "SIFTimg=cv.drawKeypoints(img,keypoint_1,image)\n",
    "\n",
    "Fast = cv.FastFeatureDetector_create()\n",
    "keypoint_2 = Fast.detect(img,None)\n",
    "Fastimg=cv.drawKeypoints(img,keypoint_2,image)\n",
    "\n",
    "ORB=cv.ORB_create()\n",
    "keypoint_3 = ORB.detect(img,None)\n",
    "ORBimg = cv.drawKeypoints(img,keypoint_3,image)\n",
    "\n",
    "#cv.imshow('Harris',Harris)\n",
    "cv.imshow('SIFT',SIFTimg)\n",
    "cv.imshow('FAST',Fastimg)\n",
    "cv.imshow('ORB',ORBimg)\n",
    "\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "根据三个算法的角点监测结果来看，ORB算子识别效果比较好，而且较准确，SIFT次之，但是SIFT有的时候会识别到棋盘方格的中心位置，FAST不知道是不是我使用的错误，只检查了边缘的角点。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
